package com.ruoyi.order.service.impl;

import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.Comment;
import com.ruoyi.order.domain.PositionRecorder;
import com.ruoyi.order.mapper.CommentMapper;
import com.ruoyi.order.mapper.PositionRecorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.order.mapper.TripOrderMapper;
import com.ruoyi.order.domain.TripOrder;
import com.ruoyi.order.service.ITripOrderService;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-11
 */
@Service
public class TripOrderServiceImpl extends ServiceImpl<TripOrderMapper, TripOrder> implements ITripOrderService {

    @Autowired
    private TripOrderMapper tripOrderMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private PositionRecorderMapper positionRecorderMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public TripOrder selectTripOrderByOrderId(Long orderId) {
        return tripOrderMapper.selectTripOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param tripOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<TripOrder> selectTripOrderList(TripOrder tripOrder) {
        return tripOrderMapper.selectTripOrderList(tripOrder);
    }

    /**
     * 新增订单管理
     *
     * @param tripOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertTripOrder(TripOrder tripOrder) {
                tripOrder.setCreateTime(DateUtils.getNowDate());
            return tripOrderMapper.insertTripOrder(tripOrder);
    }

    /**
     * 修改订单管理
     *
     * @param tripOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateTripOrder(TripOrder tripOrder) {
                tripOrder.setUpdateTime(DateUtils.getNowDate());
        return tripOrderMapper.updateTripOrder(tripOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTripOrderByOrderIds(Long[] orderIds) {
        
            LambdaUpdateWrapper<TripOrder> wrapper;
            for (Long id : orderIds) {
                wrapper = Wrappers.lambdaUpdate(TripOrder.class)
                .eq(TripOrder::getOrderId, id)
                    .set(TripOrder::getDelFlag, "1");
                super.update(new TripOrder(), wrapper);
            }
            return 1;
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTripOrderByOrderId(Long orderId) {
        return tripOrderMapper.deleteTripOrderByOrderId(orderId);
    }

    /**
     * 按订单号去查寻对于该订单号评价
     *
     * @param orderId 订单管理id
     * @param tripOrder 订单管理
     * */
    @Override
    public int selectComment(Long orderId, Long tripOrder){
        return tripOrderMapper.selectComment(orderId, tripOrder);
    }

    @Override
    public List<Comment> getReviewsByOrderId(Long orderId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getOrderId, orderId);
        return commentMapper.selectCommentsByOrderId(orderId);
    }

    @Override
    public List<PositionRecorder> getPositionsByOrderId(Long orderId) {
        return positionRecorderMapper.selectPositionsByOrderId(orderId);
    }
}



